package Handler;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import DAO.*;
import Modelo.*;

@WebServlet("/DependenciaHandler")
public class DependenciaHandler extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	private static String ADD = "/DependenciaAdd.jsp";
    private static String Edit = "/Dependenciaedit.jsp";
    private static String DependenciaRecord="/ListDependencia.jsp";
    private DependenciaDao dao;
    
    public DependenciaHandler() {
        super();
        dao = new DependenciaDao();
    }

	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String redirect="";       
        String action = request.getParameter("action");
        
		if (action.equalsIgnoreCase("editform")){         
		        redirect = Edit;            
		}
		else if (action.equalsIgnoreCase("addnew")){         
		        redirect = ADD;            
		}else if (action.equalsIgnoreCase("listDependencia")) {
			redirect = DependenciaRecord;
			request.setAttribute("Dependencia", dao.getAllDependencias());
		} 
		
		 RequestDispatcher rd = request.getRequestDispatcher(redirect);
		 rd.forward(request, response);
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String redirect="";
        String combId = request.getParameter("DependenciaId");        
        String action = request.getParameter("action");
        
		if (action.equalsIgnoreCase("insert")) {
			Dependencia objDependencia = new Dependencia();
			objDependencia.setNombre_Dependencia(request.getParameter("Nombre_Dependencia"));
			dao.addDependencia(objDependencia);
			redirect = DependenciaRecord;
			request.setAttribute("Dependencia", dao.getAllDependencias());
			System.out.println("Record Added Successfully");
		}

		else if (action.equalsIgnoreCase("delete")) {
			String Dependencia_Id = request.getParameter("DependenciaId");
			int lugid = Integer.parseInt(Dependencia_Id);
			dao.removeDependencia(lugid);
			redirect = DependenciaRecord;
			request.setAttribute("Dependencia", dao.getAllDependencias());
			System.out.println("Record Deleted Successfully");

		} else if (action.equalsIgnoreCase("edit")) {
			String Dependencia_Id = request.getParameter("DependenciaId");
			int pegid = Integer.parseInt(Dependencia_Id);
			Dependencia objDependencia = new Dependencia();
			objDependencia.setDependenciaId(pegid);
			objDependencia.setNombre_Dependencia(request.getParameter("Nombre_Dependencia"));
			dao.editDependencia(objDependencia);
			request.setAttribute("Dependencia", objDependencia);
			redirect = DependenciaRecord;
			System.out.println("Record updated Successfully");

		} else if (action.equalsIgnoreCase("listDependencia")) {
			redirect = DependenciaRecord;
			request.setAttribute("Dependencia", dao.getAllDependencias());
		} else {
			redirect = ADD;
		}

    RequestDispatcher rd = request.getRequestDispatcher(redirect);
    rd.forward(request, response);
	}

}
